#!/usr/bin/env node

const fs = require('fs');
const path = require('path');

console.log('🧪 Testing API Documentation Generator plugin...');

// 检查必要的文件是否存在
const requiredFiles = [
    'package.json',
    'src/extension.ts',
    'out/extension.js',
    'tsconfig.json'
];

let allFilesExist = true;

requiredFiles.forEach(file => {
    const filePath = path.join(__dirname, file);
    if (fs.existsSync(filePath)) {
        console.log(`✓ ${file} exists`);
    } else {
        console.log(`✗ ${file} missing`);
        allFilesExist = false;
    }
});

// 检查package.json内容
try {
    const packageJson = JSON.parse(fs.readFileSync(path.join(__dirname, 'package.json'), 'utf8'));
    
    if (packageJson.name === 'api-doc-generator') {
        console.log('✓ package.json has correct name');
    } else {
        console.log('✗ package.json name is incorrect');
        allFilesExist = false;
    }
    
    if (packageJson.contributes && packageJson.contributes.commands) {
        console.log('✓ package.json has commands configuration');
        
        const commands = packageJson.contributes.commands;
        const expectedCommands = ['api-doc-generator.generateDocs', 'api-doc-generator.exportMarkdown'];
        
        expectedCommands.forEach(cmd => {
            if (commands.find(c => c.command === cmd)) {
                console.log(`✓ Command ${cmd} is registered`);
            } else {
                console.log(`✗ Command ${cmd} is missing`);
                allFilesExist = false;
            }
        });
    } else {
        console.log('✗ package.json missing commands configuration');
        allFilesExist = false;
    }
    
} catch (error) {
    console.log('✗ Error reading package.json:', error.message);
    allFilesExist = false;
}

// 检查编译后的文件
try {
    const outDir = path.join(__dirname, 'out');
    if (fs.existsSync(outDir)) {
        const files = fs.readdirSync(outDir);
        if (files.length > 0) {
            console.log('✓ Compiled files exist in out/ directory');
        } else {
            console.log('✗ No compiled files found');
            allFilesExist = false;
        }
    } else {
        console.log('✗ out/ directory missing');
        allFilesExist = false;
    }
} catch (error) {
    console.log('✗ Error checking compiled files:', error.message);
    allFilesExist = false;
}

if (allFilesExist) {
    console.log('\n🎉 API Documentation Generator plugin is ready!');
    console.log('\nTo test the plugin:');
    console.log('1. Open this folder in VSCode');
    console.log('2. Press F5 to start debugging');
    console.log('3. In the new window, open a project with API code');
    console.log('4. Press Ctrl+Shift+P and type "Generate API Documentation"');
    console.log('5. Press Ctrl+Shift+P and type "Export API Docs to Markdown"');
    console.log('\nSupported languages:');
    console.log('- Java (Spring Boot)');
    console.log('- Python (Flask, FastAPI)');
    console.log('- JavaScript (Express.js)');
    console.log('- TypeScript (NestJS, Express.js)');
    console.log('\nFeatures:');
    console.log('- Automatic API endpoint detection');
    console.log('- Parameter parsing and documentation');
    console.log('- Code example generation');
    console.log('- Markdown export');
} else {
    console.log('\n❌ Plugin structure has issues. Please check the missing files.');
} 